package com.lovo.Atm;

import javax.swing.*;

import com.lovo.Stream.HomeWork;
import com.lovo.Util;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import com.lovo.Stream.SerializeUtil;

public class Land extends JPanel implements ActionListener {
    private Home frame;
    private JLabel account, password;
    private JTextField acc;
    private JPasswordField pass;
    private Icon btnImg = Util.iconImg("/images/buttonLand.jpg");
    private Icon backIm = Util.iconImg("/images/buttonOff.jpg");
    private JButton btnImages;
    private JButton backImg;
    private int count = 3;

    public Land(Home frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //账号和单行文本框
        account = new JLabel("账户：");
        account.setFont(new Font("宋体", Font.BOLD, 20));
        account.setBounds(300, 240, 70, 30);
        this.add(account);

        acc = new JTextField();
        acc.setBounds(380, 240, 100, 30);
        this.add(acc);

        //密码和密码文本框
        password = new JLabel("密码：");
        password.setFont(new Font("宋体", Font.BOLD, 20));
        password.setBounds(540, 240, 70, 30);
        this.add(password);

        pass = new JPasswordField();
        pass.setBounds(620, 240, 100, 30);
        this.add(pass);

        //登录按钮
        btnImages = new JButton(btnImg);
        btnImages.setBounds(0, 420, 140, 50);
        this.add(btnImages);
        btnImages.addActionListener(this);

        //退出程序按钮
        backImg = new JButton(backIm);
        backImg.setBounds(870, 420, 140, 50);
        this.add(backImg);
        backImg.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnImages) {
            String account = acc.getText();
            String pwd = String.valueOf(pass.getPassword());
            for (int i = 0; i < frame.getUser().size(); i++) {
                //每次循环清空输入框
                if (frame.getUser().get(i).getName().equals(account) && frame.getUser().get(i).getPassword().equals(pwd)) {
                    JOptionPane.showMessageDialog(this, "登录成功，欢迎使用!");
                    frame.switchPanel("menu");
                    frame.setLoginUser(frame.getUser().get(i));
                    frame.getFindPanel().set(frame.getLoginUser().getBlance());
                    count = 3;
                    acc.setText("");
                    pass.setText("");
                    return;
                }
            }
            count--;
            if (count == 0) {
                JOptionPane.showMessageDialog(this, "次数用完，没收银行卡！");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "账号或密码有误，您还有" + (count) + "次机会~");
        }
        if (e.getSource() == backImg) {
            JOptionPane.showMessageDialog(this, "谢谢使用，欢迎下次光临~");
            System.exit(0);
        }
    }
}
